﻿using System;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace PyramidShadow
{
    class Body
    {
        private Vector2 lastMove;
        private Rectangle tmp;
        // Animation representing the body
        private Texture2D PlayerTexture;
        // Position of the body        
        public Rectangle boundingBox;
        // State of the body
        private bool Active;
        private Level level;
        private const BoxType type = BoxType.Body;

        // Get the width of the body
        public int Width
        {
            get { return PlayerTexture.Width; }
        }

        // Get the height of the body
        public int Height
        {
            get { return PlayerTexture.Height; }
        }

        public Body(Texture2D texture, Vector2 startPosition, Level level)
        {
            this.level = level;
            PlayerTexture = texture;
            boundingBox = new Rectangle ((int)startPosition.X,(int)startPosition.Y,PlayerTexture.Width,PlayerTexture.Height);
            Active = true;
            tmp = new Rectangle();
            lastMove = new Vector2();
        }

        
        public void Update(GameTime gameTime, KeyboardState currentKeyboardState)
        {
            return;// testing purposes

            lastMove.X = 0;
            lastMove.Y = 0;

            lastMove.X = boundingBox.X;
            lastMove.Y = boundingBox.Y;

            tmp = boundingBox;
            // Use the Keyboard 
            if (currentKeyboardState.IsKeyDown(Keys.Left))
            {
                tmp.X = boundingBox.X - level.playerMoveSpeed;
                if (!level.kollision.collide(tmp,type))
                    boundingBox.X -= level.playerMoveSpeed;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Right))
            {
                tmp.X = boundingBox.X + level.playerMoveSpeed;
                if (!level.kollision.collide(tmp, type))
                    boundingBox.X += level.playerMoveSpeed;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Up))
            {
                tmp.Y = boundingBox.Y - level.playerMoveSpeed;
                if (!level.kollision.collide(tmp, type))
                    boundingBox.Y -= level.playerMoveSpeed;
            }
            if (currentKeyboardState.IsKeyDown(Keys.Down) && tmp.Y >= 0)
            {
                tmp.Y = boundingBox.Y + level.playerMoveSpeed;
                if (!level.kollision.collide(tmp, type))
                    boundingBox.Y += level.playerMoveSpeed;
            }

            lastMove.X -=  boundingBox.X; 
            lastMove.Y -= boundingBox.Y;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(PlayerTexture,boundingBox,Color.White);
        }

        public void jumpBack()
        {
            boundingBox.X += (int)lastMove.Y;
            boundingBox.Y += (int)lastMove.X;
        }
    }
}
